package com.kmt.plaza.controller;

import com.kmt.plaza.entity.ProductComments;
import com.kmt.plaza.sevice.UserService;
import com.kmt.plaza.entity.User;
import com.kmt.plaza.utils.UpPhotoNameUtils;
import com.kmt.plaza.vo.ResStatus;
import com.kmt.plaza.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Api(value = "提供用户登录和注册",tags = "用户管理")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 用户注册接口
     * 作者：王伟
     * @param user
     * @return
     */
    @ApiOperation("⽤户注册接⼝")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username", value = "⽤户注册账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password", value = "⽤户注册密码",required = true)
    })
    @PostMapping("/regist")
    public ResultVO regist(@RequestBody User user){
        ResultVO resultVO = userService.userResgit(user.getUserName(),user.getPassWord());
        return resultVO;
    }
    /**
     * 用户登录接口
     * @param userName
     * @param passWord
     * @return
     */
    @ApiOperation(value = "用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "username",value = "用户登录账号",required = true),
            @ApiImplicitParam(dataType = "String",name = "password",value = "用户登录密码",required = false,defaultValue = "111111")
    })
    @GetMapping("/login")
    public ResultVO login(@RequestParam("username") String userName,
                          @RequestParam(value = "password",defaultValue = "111111")String passWord){
        return userService.checkLogin(userName,passWord);
    }
    //采用PostMapping
    /**
     * 用户修改个人头像接口
     * @param file
     * @param request
     * @param userId
     * @param status
     * @return
     */
    @ApiOperation(value = "用户头像修改接口")
    @PostMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   HttpServletRequest request, Long userId, String status) {
        //@RequestParam("file") MultipartFile file为接收图片参数
        //Long userId,String status 用户Id和状态

        try {
            byte[] bytes = file.getBytes();
            String imageFileNames = file.getOriginalFilename();
            String imageFileName = System.currentTimeMillis() + imageFileNames;
//            String fileName = UpPhotoNameUtils.getPhotoName("UserAvatar",imageFileName);
//            Path path = Paths.get("F:\\plaza\\communal\\src\\main\\java\\com\\kmt\\plaza\\utils\\imgs\\" + fileName);
            Path path = Paths.get("F:\\particles\\plaza_static\\UserAvatar\\" + imageFileName);
            Files.write(path, bytes);//写入文件
            String avatar_url=imageFileName;
            userService.updateAvatar(userId,avatar_url);//dao层方法
            System.out.println(imageFileName+"\n");
            return imageFileName;//返回文件名字
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 用户修改个人信息接口
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户信息接口")
    @PutMapping(value = "/updateUserInfo")
    public ResultVO updateUserInfo(@RequestBody User user)
    {
        if(user.getUserSex().equals("选项1")){
            user.setUserSex("M");
        }else{
            user.setUserSex("F");
        }
        System.out.println("修改用户信息");
        int result = userService.updateUserInfo(user);
        if(result==1){
            return new ResultVO(ResStatus.OK,"修改成功",result);
        }else{
            return new ResultVO(ResStatus.NO,"修改失败",null);
        }
        // 查询所有用户信息

    }
    /**
     * 管理员按关键字搜索用户接口
     * @param userName
     * @return
     */
    @ApiOperation(value = "按关键字查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "userName",value = "用户id",required =false),
    })
    @GetMapping("/queryKey")
    public ResultVO queryKey(@RequestParam("userName") String userName){
        ResultVO resultVO = userService.queryKey(userName);
        return resultVO;
    }
    /**
     * 用户信息展示接口
     * @return
     */
    @ApiOperation(value = "用户信息展示")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer",name = "userId",value = "用户id",required =true),
    })
    @GetMapping("/userInfoList")
    public ResultVO userInfoList(Integer userId){
        ResultVO resultVO = userService.userInfoList(userId);
        return resultVO;
    }
    /**
     * 管理员删除用户接口
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/deleteUser/{userId}")
    public int deleteUser(@PathVariable Integer userId)
    {
        // 删除该条用户信息
        int user = userService.deleteUserchanger(userId);
        return user;
    }
    /**
     * 管理员查询用户分页
     */
    @ApiOperation(value = "用户分页列表查询")
    @GetMapping(value = "/findAll")
    public ResultVO findAll(int pageNum,int limit)
    {
        return userService.findAll(pageNum,limit);
    }

    /**
     * 根据用户id查询用户评论信息
     */
    @ApiOperation(value = "个人评论查询")
    @GetMapping(value = "/ProductCommentsByuserId")
    public ResultVO ProductCommentsByuserId(String userId,int pageNum,int limit)
    {
        return userService.selectAllByUserId(userId,pageNum,limit);
    }
    /**
     * 删除个人评论
     */
    @ApiOperation(value = "删除个人评论接口")
    @DeleteMapping(value = "/deleteCommon/{commId}")
    public ResultVO deleteShopCart(@PathVariable("commId") Integer commId,@RequestHeader("token")String token) {
        return userService.deleteProductComment(commId);
    }


}
